package com.example.demo.controller;

import com.example.demo.Util.UserUtil;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2022-09-20
 * Time:17:17
 */
@RestController
public class UserController {
    @Autowired
    private UserUtil userUtil;
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest req,
                        HttpServletResponse resp,
                        String username, String password){
        //        先判断用户密码是否为空
        if(!StringUtils.hasLength(username)||
                !StringUtils.hasLength(password)){
            return "<h1>用户名或密码为空</h1>";
        }
        //      查看用户是否存在
        User user = userUtil.UserIsExist(username);
        if (user == null){
            return "<h1>用户不存在</h1>";
        }

        //        判断密码是否正确
        if (!password.equals(user.getPassword())){
            return "<h1>密码错误请重新输入</h1>";
        }
        HttpSession session = req.getSession(true);
        if (session != null){
            session.setAttribute("user",user);
        }

        System.out.println(session.getAttribute("user").toString());
        if (user.getUserType() == 1){
            try {
                resp.sendRedirect("problemsList.html");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }else {
            try {
                resp.sendRedirect("adminList.html");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return "<h1>登录成功!!!/h1>";
    }
//    用户注册
    @RequestMapping("/register")
    public String addUser(HttpServletResponse resp,String username, String password, String repassword){
//        先判断用户密码是否为空
        if(!StringUtils.hasLength(username)||
                !StringUtils.hasLength(password)){
            return "<h1>用户名或密码为空</h1>";
        }

//      判断两次输入的的密码是否一致
        if (!password.equals(repassword)){
            return "<h1>两次输入的密码不一致</h1>";
        }
       User user = userUtil.UserIsExist(username);
        if(user != null){
            return "<h1>该用户名已存在</h1>";
        }
        user = new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.addUser(user);
        try {
            resp.sendRedirect("/login.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "<h1>注册成功!!!!</h1>";
    }

////    修改密码
    @RequestMapping("/updateUser")
    public String updateUser(HttpServletResponse resp,
                             String username,String password,
                             String newPassword,String reNewPassword){
//       验证参数是否合格
        if (!StringUtils.hasLength(username) ||
            !StringUtils.hasLength(password) ||
            !StringUtils.hasLength(newPassword) ||
            !StringUtils.hasLength(reNewPassword)){
            return "<h1>用户名或密码为空</h1>";
        }

//      查看用户是否存在
        //      查看用户是否存在

        User user = userUtil.UserIsExist(username);
        if (user == null){
            return "<h1>用户不存在</h1>";
        }

//        判断密码是否正确

        if (!password.equals(user.getPassword())){
            return "<h1>密码错误请重新输入</h1>";
        }

//        判断两次输入的密码是否一致
        if (!newPassword.equals(reNewPassword)){
            return "<h1>两次输入的密码不同请重新输入</h1>";
        }

        userService.updateByUsername(username,newPassword);
        try {
            resp.sendRedirect("/login.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "<h1>密码修改成功</h1>";
    }

//    注销账户
    @RequestMapping("/deleteUser")
    public String deleteUser(HttpServletResponse resp,
                             String username,String password){
        //        先判断用户密码是否为空
        if(!StringUtils.hasLength(username)||
                !StringUtils.hasLength(password)){
            return "<h1>用户名或密码为空</h1>";
        }

        //      查看用户是否存在

        User user = userUtil.UserIsExist(username);
        if (user == null){
            return "<h1>用户不存在</h1>";
        }

//        判断面膜是否正确
        if (!password.equals(user.getPassword())){
            return "<h1>密码错误请重新输入</h1>";
        }

//        注销用户
        userService.deleteUser(username,password);
        try {
            resp.sendRedirect("/login.html");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "<h1>账户注销成功!!!</h1>";
    }
}
